package org.qs.webservice.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.HeaderParam;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;

import org.qs.webservice.exception.QSReadException;
import org.qs.webservice.service.BookService;
import org.qs.webservice.service.BookShelfService;
import org.qs.webservice.util.IReadUtils;
import org.springframework.stereotype.Component;


@Component
@Path("/v{version}/shelf")
public class BookShelfRestController {
	
	@Resource
	private BookShelfService bookShelfService;
	/**
	 * 获取书籍详情
	 * @param book_id
	 * @return
	 */
	@POST
    @Path("/add")
	public String addShelf(@FormParam("book_id")int book_id,@HeaderParam("token")String token,
			@HeaderParam("info-uuid")String device_identifier){
		try {
			String ret = bookShelfService.updateShelf(book_id,token,device_identifier);
			return ret;
		} catch (Exception e) {
			Map<String,Object> errorJson = new HashMap<String,Object>();
			errorJson.put("book_id", book_id);
			QSReadException.printf(e, "添加书架异常",errorJson);
			Map<String,Object> tokenMap =  new HashMap<String,Object>();
			tokenMap.put("code", 403);
			tokenMap.put("reason","服务器错误");
			return IReadUtils.beanToJson(tokenMap);
		}
	}
	@POST
    @Path("/delete")
	public String deleteShelf(@FormParam("mark_id")List<String> mark_ids,@FormParam("book_id")String book_id,@FormParam("mark_ids")String mark_id_str,@HeaderParam("token")String token,@HeaderParam("info-uuid")String device_identifier){
		try {
			System.out.println(book_id);
			String ret =  bookShelfService.deleteShelf(mark_ids,token,device_identifier,mark_id_str,book_id);
			return ret;
		} catch (Exception e) {
			Map<String,Object> errorJson = new HashMap<String,Object>();
			errorJson.put("mark_ids", mark_ids);
			QSReadException.printf(e, "添加书架异常",errorJson);
			Map<String,Object> tokenMap =  new HashMap<String,Object>();
			tokenMap.put("code", 403);
			tokenMap.put("reason","服务器错误");
			return IReadUtils.beanToJson(tokenMap);
		}
	}
	@GET
    @Path("/list")
	public String list(@QueryParam("pages")int pages,@QueryParam("contents")int contents,@HeaderParam("token")String token,@HeaderParam("info-uuid")String device_identifier){
		try {
			String ret = bookShelfService.listShelf(pages,contents,token,device_identifier);
			return ret;
		} catch (Exception e) {
			Map<String,Object> errorJson = new HashMap<String,Object>();
			errorJson.put("pages", pages);
			errorJson.put("contents", contents);
			QSReadException.printf(e, "添加书架异常",errorJson);
			Map<String,Object> tokenMap =  new HashMap<String,Object>();
			tokenMap.put("code", 403);
			tokenMap.put("reason","服务器错误");
			return IReadUtils.beanToJson(tokenMap);
		}
	}
	
	
}
